package com.lazyboyl.sharding.proxy.net.frontend;

import com.lazyboyl.sharding.proxy.net.proto.mysql.*;
import com.lazyboyl.sharding.proxy.net.proto.parse.ServerParse;
import com.lazyboyl.sharding.proxy.net.proto.util.ByteUtil;
import com.lazyboyl.sharding.proxy.net.proto.util.Fields;
import com.lazyboyl.sharding.proxy.net.proto.util.PacketUtil;
import com.lazyboyl.sharding.proxy.net.proto.util.StringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ServerQueryHandler implements FrontendQueryHandler {

    private FrontendConnection connection;

    public ServerQueryHandler(FrontendConnection connection) {
        this.connection = connection;
    }

    private static final Logger logger = LoggerFactory.getLogger(ServerQueryHandler.class);

    @Override
    public void query(String sql) {
        logger.info("sql = " + sql);
        int rs = ServerParse.parse(sql);
        switch (rs & 0xff) {
            case ServerParse.SET:
                SetHandler.handle(sql, connection, rs >>> 8);
                // 实现删除数据的操作
            case ServerParse.DELETE:

                break;
            // 实现插入数据的操作
            case ServerParse.INSERT:

                break;
            // 展示数据库的列表
            case ServerParse.SHOW:
                ShowHandler.handle(sql, connection, rs >>> 8);
                break;
            // navicat发起终止的操作请求
            case ServerParse.KILL:
                KillHandler.handle(sql, connection, rs >>> 8);
                break;
            // navicat发起的查询请求
            case ServerParse.SELECT:
                SelectHandler.handle(sql, connection, rs >>> 8);
                break;
            // 我们使用USE xxx数据库的时候的操作请求
            case ServerParse.USE:
                UseHandler.handle(sql, connection, rs >>> 8);
                break;


        }

    }
}
